﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GraphicsEditor
{
    public partial class RotateForm : Form
    {
        public RotateForm()
        {
            InitializeComponent();
        }

        ImageForm imf;

        public RotateForm(ImageForm imf)
        {
            this.imf = imf;
            InitializeComponent();

            Bitmap myBitmap = imf.GetBitmap();
            lblInformation.Text = ("The image is " + myBitmap.Width.ToString() + " wide and " + myBitmap.Height.ToString() + " tall.");
            txtWidth.Text = (myBitmap.Width / 2).ToString();
            txtHeight.Text = (myBitmap.Height / 2).ToString();
        }



        private void btnClockwiseRotation_Click(object sender, EventArgs e)
        {
            Bitmap currentImage = imf.GetBitmap();
            imf.BitmapTask();
            if (radioBtn90.Checked == true)
            {
                
                Rotate.Rotate90(currentImage);
            }
            else if (radioBtn180.Checked == true)
            {
                currentImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
            }
            else if (radioBtn270.Checked == true)
            {
                currentImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
            }
            imf.SetBitmap(currentImage);
        }

        private void btnFlip_Click(object sender, EventArgs e)
        {
            if (radioBtnHorizontalFlip.Checked == true)
            {
                imf.BitmapTask();
                Bitmap currentImage = imf.GetBitmap();
                currentImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
                imf.SetBitmap(currentImage);
            }
            if (radioBtnVerticalFlip.Checked == true)
            {
                imf.BitmapTask();
                Bitmap currentImage = imf.GetBitmap();
                currentImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                imf.SetBitmap(currentImage);
            }
            if (radioBtnBoth.Checked == true)
            {
                imf.BitmapTask();
                Bitmap currentImage = imf.GetBitmap();
                currentImage.RotateFlip(RotateFlipType.RotateNoneFlipXY);
                imf.SetBitmap(currentImage);
            }

        }

        private void btnRotateandFlip_Click(object sender, EventArgs e)
        {
            btnFlip.PerformClick();
            btnClockwiseRotation.PerformClick();
        }

        private void btnFreeRotate_Click(object sender, EventArgs e)
        {
            float rotate = float.Parse(txtFreeDegreeRotation.Text);
            float cos = Math.Abs((float)Math.Cos(rotate));
            float sin = Math.Abs((float)Math.Sin(rotate));
            int width = imf.GetBitmap().Width;
            int height = imf.GetBitmap().Height;
            int newWidth = (int)(width * cos + height * sin);
            int newHeight = (int)(width * sin + height * cos);
            Console.WriteLine(newWidth.ToString() + " <-w h-> " + newHeight.ToString());
            imf.BitmapTask();
            Bitmap rotatedImage = new Bitmap(newWidth,newHeight );
            Graphics g = Graphics.FromImage(rotatedImage);

            //Moving the default point of rotation from upper left corner to our new point of rotation.            
            g.TranslateTransform(float.Parse(txtWidth.Text), float.Parse(txtHeight.Text));
            //Actually rotating image here.
            g.RotateTransform(float.Parse(txtFreeDegreeRotation.Text));
            //Must move the coordinate system back to the original location.
            g.TranslateTransform(-float.Parse(txtWidth.Text), -float.Parse(txtHeight.Text));

            //Draw the image starting at the upper left hand corner.
            g.DrawImage(imf.GetBitmap(), new Point(0, 0));
            imf.SetBitmap(rotatedImage);
        }








    }
}

